package com.ruoyi.project.wenyanwen.tag.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.wenyanwen.tag.domain.ArticleTag;
import com.ruoyi.project.wenyanwen.tag.service.IArticleTagService;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容标签Controller
 *
 * @author shixiangwen
 * @date 2020-08-03
 */
@Controller
@RequestMapping("/wenyanwen/tag")
public class ArticleTagController extends BaseController {

    protected static org.apache.commons.logging.Log logger = LogFactory.getLog(ArticleTagController.class);

    private String prefix = "wenyanwen/tag";

    @Autowired
    private IArticleTagService articleTagService;

    @RequiresPermissions("wenyanwen:tag:view")
    @GetMapping()
    public String tag() {
        return prefix + "/tag";
    }

    /**
     * 查询内容标签树列表
     */
    @RequiresPermissions("wenyanwen:tag:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ArticleTag> list(ArticleTag articleTag) {
        List<ArticleTag> list = articleTagService.selectArticleTagList(articleTag);
        return list;
    }

    /**
     * 导出内容标签列表
     */
    @RequiresPermissions("wenyanwen:tag:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ArticleTag articleTag) {
        List<ArticleTag> list = articleTagService.selectArticleTagList(articleTag);
        ExcelUtil<ArticleTag> util = new ExcelUtil<ArticleTag>(ArticleTag.class);
        return util.exportExcel(list, "tag");
    }

    /**
     * 新增内容标签
     */
    @GetMapping(value = {"/add/{tagId}", "/add/"})
    public String add(@PathVariable(value = "tagId", required = false) Long tagId, ModelMap mmap) {
        if (StringUtils.isNotNull(tagId)) {
            mmap.put("articleTag", articleTagService.selectArticleTagById(tagId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存内容标签
     */
    @RequiresPermissions("wenyanwen:tag:add")
    @Log(title = "内容标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ArticleTag articleTag) {
        return toAjax(articleTagService.insertArticleTag(articleTag));
    }

    /**
     * 修改内容标签
     */
    @GetMapping("/edit/{tagId}")
    public String edit(@PathVariable("tagId") Long tagId, ModelMap mmap) {
        ArticleTag articleTag = articleTagService.selectArticleTagById(tagId);
        mmap.put("articleTag", articleTag);
        return prefix + "/edit";
    }

    /**
     * 修改保存内容标签
     */
    @RequiresPermissions("wenyanwen:tag:edit")
    @Log(title = "内容标签", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ArticleTag articleTag) {
        return toAjax(articleTagService.updateArticleTag(articleTag));
    }

    /**
     * 删除
     */
    @RequiresPermissions("wenyanwen:tag:remove")
    @Log(title = "内容标签", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{tagId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("tagId") Long tagId) {
        return toAjax(articleTagService.deleteArticleTagById(tagId));
    }

    /**
     * 选择内容标签树
     */
    @GetMapping(value = {"/selectTagTree/{tagId}", "/selectTagTree/"})
    public String selectTagTree(@PathVariable(value = "tagId", required = false) Long tagId, ModelMap mmap) {
        if (StringUtils.isNotNull(tagId)) {
            mmap.put("articleTag", articleTagService.selectArticleTagById(tagId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载内容标签树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = articleTagService.selectArticleTagTree();
        return ztrees;
    }
}
